
let captcha
async function render() {
    const res = await axios({
        url: '/captcha/image',
    })
    // console.log(res.data.data.key, '12321')
    console.log(res.data.data)
    document.querySelector('.item img').src = res.data.data.base64
    captcha = res.data.data.key

}
render()


// 注册获取验证码点击事件
document.querySelector('.verify').addEventListener('click', async function (e) {
    e.preventDefault()
    // showToast('请输入正确的手机号')
    const { mobile, captchaCode, smsCode } = serialize(document.querySelector('form'), { hash: true, empty: true })
    // console.log(data)
    const moblieone = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/

    if (!mobile.length) {
        showToast('请先输入的手机号')
        return
    }

    if (!moblieone.test(mobile)) {
        showToast('请输入正确的手机号')
        return
    }

    if (!captchaCode.length) {
        showToast('请先输入的图行验证码')
        return
    }

    const res = await axios({
        method: 'post',
        url: '/captcha/sendSmsCaptcha',

        data: {
            form: {
                mobile,
                captchaCode,
                captchaKey: captcha
            }
        }
    })
    console.log(res)
    showToast(res.data.message)
    if (res.data.status !== 200) {
        render()
    }

})


//给登录按钮注册点击事件
document.querySelector('.submit').addEventListener('click', async function (e) {
    e.preventDefault()
    const { mobile, captchaCode, smsCode } = serialize(document.querySelector('form'), { hash: true, empty: true })
    const moblieone = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/

    if (!mobile.length) {
        showToast('请先输入的手机号')
        return
    }

    if (!moblieone.test(mobile)) {
        showToast('请输入正确的手机号')
        return
    }

    if (!smsCode.length) {
        showToast('请输入短信验证码')
        return
    }

    const res = await axios({
        method: 'post',
        url: '/passport/login',
        data: {
            form: {
                isParty: false,
                mobile,
                partyData: {},
                smsCode: '246810'
            }
        }
    })
    console.log(res)

    showToast('登录成功')
    localStorage.setItem('mobile', mobile)
    localStorage.setItem('token', res.data.data.token)
    localStorage.setItem('userId', res.data.data.userId)

    setTimeout(() => {
        location.href = './my.html'
    }, 1500)


})



